package com.wl.api.controller.system;

import java.util.function.Predicate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wl.api.common.utils.BeanUtils;
import com.wl.api.common.utils.ErrorCodeEnum;
import com.wl.api.common.utils.JsonObject;
import com.wl.api.common.utils.ObjectUtil;
import com.wl.api.common.utils.StringUtil;
import com.wl.api.controller.base.CommonController;
import com.wl.api.dto.base.CommonUserDTO;
import com.wl.api.dto.system.OmsIndexInformationDTO;
import com.wl.api.model.system.OmsIndexInformation;
import com.wl.api.service.business.IOmsIndexInformationService;
/**
 * 
 * @author chenj
   @desc 首页oms资讯信息管理
 *
 */

@RestController
@RequestMapping("/admin/oms/index")
public class IndexManageController extends CommonController {
	
	@Autowired
	IOmsIndexInformationService indexInformationService;
	
	@RequestMapping(value = "/manage", method = RequestMethod.POST)
	JsonObject create(OmsIndexInformationDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "" , code = ""; 
		if (validate().test(param)) {
			title = ErrorCodeEnum.param_error.getMessage();
			code = ErrorCodeEnum.param_error.getError_code();
			pass = false;
		}
		if(pass) {
			initCreateUser(param);
			OmsIndexInformation entity = BeanUtils.copyProperties(param, OmsIndexInformation.class);
			indexInformationService.insert(entity);
			json.setData(entity.getId());
		}
		setJsonMessage(json, title, code, pass);
		return json;
	}
	
	@RequestMapping(value = "/manage", method = RequestMethod.PUT)
	JsonObject modify(@RequestBody  OmsIndexInformationDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "" , error_code = ""; 
		if (validate().and(s -> s.getId() == null).test(param)) {
			title = ErrorCodeEnum.param_error.getMessage();
			error_code = ErrorCodeEnum.param_error.getError_code();
			pass = false;
		}
		if(pass && !ObjectUtil.isEmpty(indexInformationService.selectOne(new EntityWrapper<OmsIndexInformation>(OmsIndexInformation.builder().status(1).id(param.getId()).build())))) {
			error_code = ErrorCodeEnum.bad_request_published_data.getError_code();
			title = ErrorCodeEnum.bad_request_published_data.getMessage();
			pass = false;
		}
		if(pass) {
			initUpdateUser(param);
			OmsIndexInformation entity = BeanUtils.copyProperties(param, OmsIndexInformation.class);
			indexInformationService.updateById(entity);
			json.setData(entity.getId());
		}
		setJsonMessage(json, title, error_code, pass);
		return json;
	}
	/** 只提供修改数据状态 */
	@RequestMapping(value = "/manage/{id}", method = RequestMethod.PUT)
	JsonObject modifyStatus(@PathVariable("id")Integer id, Integer status) throws Exception {
		JsonObject json = JsonObject.getInstance();
		indexInformationService.updateById(OmsIndexInformation.builder().updateUser(getCommonUser().getUsername()).id(id).build());
		setJsonMessage(json);
		return json;
	}
	
	@RequestMapping(value="/manage/{id}")
	JsonObject del(@PathVariable("id") Integer id) throws Exception {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "" , error_code = ""; 
		
		if(indexInformationService.selectOne(new EntityWrapper<OmsIndexInformation>(OmsIndexInformation.builder().status(0).id(id).build())) == null) {
			error_code = ErrorCodeEnum.bad_request_published_data.getError_code();
			title = ErrorCodeEnum.bad_request_published_data.getMessage();
			pass = false;
		}
		if (pass) {
			indexInformationService.deleteById(id);
		}
		setJsonMessage(json, title, error_code, pass);
		return json;
	}
	
	private void initCreateUser(OmsIndexInformationDTO param) throws Exception {
		CommonUserDTO user = getCommonUser();
		param.buildCreateMsg(user.getId(), user.getUsername());
		
	}
	private void initUpdateUser(OmsIndexInformationDTO param) throws Exception {
		CommonUserDTO user = getCommonUser();
		param.buildUpdateMsg(user.getUsername());
		
	}
	
	
	private Predicate<OmsIndexInformationDTO> validate() {
		return s -> StringUtil.isEmpty(s.getTitle()) || StringUtil.isEmpty(s.getDescription());
	}
	
	
}
